{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import datetime as dt\n",
    "from scipy import stats\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>851104</td>\n",
       "      <td>2017-01-21 22:11:48.556739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>804228</td>\n",
       "      <td>2017-01-12 08:01:45.159739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>661590</td>\n",
       "      <td>2017-01-11 16:55:06.154213</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>853541</td>\n",
       "      <td>2017-01-08 18:28:03.143765</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>864975</td>\n",
       "      <td>2017-01-21 01:52:26.210827</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id                   timestamp      group landing_page  converted\n",
       "0   851104  2017-01-21 22:11:48.556739    control     old_page          0\n",
       "1   804228  2017-01-12 08:01:45.159739    control     old_page          0\n",
       "2   661590  2017-01-11 16:55:06.154213  treatment     new_page          0\n",
       "3   853541  2017-01-08 18:28:03.143765  treatment     new_page          0\n",
       "4   864975  2017-01-21 01:52:26.210827    control     old_page          1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('./ab_data.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_old = df[(df['landing_page']=='old_page')].reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>851104</td>\n",
       "      <td>2017-01-21 22:11:48.556739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>804228</td>\n",
       "      <td>2017-01-12 08:01:45.159739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4</td>\n",
       "      <td>864975</td>\n",
       "      <td>2017-01-21 01:52:26.210827</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>936923</td>\n",
       "      <td>2017-01-10 15:20:49.083499</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7</td>\n",
       "      <td>719014</td>\n",
       "      <td>2017-01-17 01:48:29.539573</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147234</th>\n",
       "      <td>294471</td>\n",
       "      <td>718310</td>\n",
       "      <td>2017-01-21 22:44:20.378320</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147235</th>\n",
       "      <td>294473</td>\n",
       "      <td>751197</td>\n",
       "      <td>2017-01-03 22:28:38.630509</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147236</th>\n",
       "      <td>294474</td>\n",
       "      <td>945152</td>\n",
       "      <td>2017-01-12 00:51:57.078372</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147237</th>\n",
       "      <td>294475</td>\n",
       "      <td>734608</td>\n",
       "      <td>2017-01-22 11:45:03.439544</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147238</th>\n",
       "      <td>294476</td>\n",
       "      <td>697314</td>\n",
       "      <td>2017-01-15 01:20:28.957438</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>147239 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         index  user_id                   timestamp    group landing_page  \\\n",
       "0            0   851104  2017-01-21 22:11:48.556739  control     old_page   \n",
       "1            1   804228  2017-01-12 08:01:45.159739  control     old_page   \n",
       "2            4   864975  2017-01-21 01:52:26.210827  control     old_page   \n",
       "3            5   936923  2017-01-10 15:20:49.083499  control     old_page   \n",
       "4            7   719014  2017-01-17 01:48:29.539573  control     old_page   \n",
       "...        ...      ...                         ...      ...          ...   \n",
       "147234  294471   718310  2017-01-21 22:44:20.378320  control     old_page   \n",
       "147235  294473   751197  2017-01-03 22:28:38.630509  control     old_page   \n",
       "147236  294474   945152  2017-01-12 00:51:57.078372  control     old_page   \n",
       "147237  294475   734608  2017-01-22 11:45:03.439544  control     old_page   \n",
       "147238  294476   697314  2017-01-15 01:20:28.957438  control     old_page   \n",
       "\n",
       "        converted  \n",
       "0               0  \n",
       "1               0  \n",
       "2               1  \n",
       "3               0  \n",
       "4               0  \n",
       "...           ...  \n",
       "147234          0  \n",
       "147235          0  \n",
       "147236          0  \n",
       "147237          0  \n",
       "147238          0  \n",
       "\n",
       "[147239 rows x 6 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_old"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>661590</td>\n",
       "      <td>2017-01-11 16:55:06.154213</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>853541</td>\n",
       "      <td>2017-01-08 18:28:03.143765</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>679687</td>\n",
       "      <td>2017-01-19 03:26:46.940749</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8</td>\n",
       "      <td>817355</td>\n",
       "      <td>2017-01-04 17:58:08.979471</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "      <td>839785</td>\n",
       "      <td>2017-01-15 18:11:06.610965</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147234</th>\n",
       "      <td>294462</td>\n",
       "      <td>677163</td>\n",
       "      <td>2017-01-03 19:41:51.902148</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147235</th>\n",
       "      <td>294465</td>\n",
       "      <td>925675</td>\n",
       "      <td>2017-01-07 20:38:26.346410</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147236</th>\n",
       "      <td>294468</td>\n",
       "      <td>643562</td>\n",
       "      <td>2017-01-02 19:20:05.460595</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147237</th>\n",
       "      <td>294472</td>\n",
       "      <td>822004</td>\n",
       "      <td>2017-01-04 03:36:46.071379</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147238</th>\n",
       "      <td>294477</td>\n",
       "      <td>715931</td>\n",
       "      <td>2017-01-16 12:40:24.467417</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>147239 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         index  user_id                   timestamp      group landing_page  \\\n",
       "0            2   661590  2017-01-11 16:55:06.154213  treatment     new_page   \n",
       "1            3   853541  2017-01-08 18:28:03.143765  treatment     new_page   \n",
       "2            6   679687  2017-01-19 03:26:46.940749  treatment     new_page   \n",
       "3            8   817355  2017-01-04 17:58:08.979471  treatment     new_page   \n",
       "4            9   839785  2017-01-15 18:11:06.610965  treatment     new_page   \n",
       "...        ...      ...                         ...        ...          ...   \n",
       "147234  294462   677163  2017-01-03 19:41:51.902148  treatment     new_page   \n",
       "147235  294465   925675  2017-01-07 20:38:26.346410  treatment     new_page   \n",
       "147236  294468   643562  2017-01-02 19:20:05.460595  treatment     new_page   \n",
       "147237  294472   822004  2017-01-04 03:36:46.071379  treatment     new_page   \n",
       "147238  294477   715931  2017-01-16 12:40:24.467417  treatment     new_page   \n",
       "\n",
       "        converted  \n",
       "0               0  \n",
       "1               0  \n",
       "2               1  \n",
       "3               1  \n",
       "4               1  \n",
       "...           ...  \n",
       "147234          0  \n",
       "147235          0  \n",
       "147236          0  \n",
       "147237          0  \n",
       "147238          0  \n",
       "\n",
       "[147239 rows x 6 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_new = df[(df['landing_page']=='new_page')].reset_index()\n",
    "df_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17498 17739\n",
      "0.11884079625642663 0.12047759085568362\n"
     ]
    }
   ],
   "source": [
    "#在这里说明两个banner页面这里设置的一样的点击量，那么主要看的是这里的转化率了\n",
    "df_new_sum =df_new['converted'].count() #计算新banner页总的点击量\n",
    "df_new_suss = df_new.loc[:,'converted'].sum() #计算banner页转化的总数量\n",
    "df_new_chang = df_new_suss/df_new_sum #计算新banner页面转化率\n",
    "df_new_chang \n",
    "\n",
    "df_old_sum =df_old['converted'].count() #计算新banner页总的点击量\n",
    "df_old_suss = df_old.loc[:,'converted'].sum() #计算banner页转化的总数量\n",
    "df_old_chang = df_old_suss/df_old_sum #计算新banner页面转化率\n",
    "\n",
    "print(df_new_suss,df_old_suss) #打印两个banner页面的下单量\n",
    "print(df_new_chang,df_old_chang)#打印两个banner页面的下单率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.368338490064514\n"
     ]
    }
   ],
   "source": [
    "#我们的原建设是H0是两者之间没有区别，由于新旧页面的下单率都满足二项分布即\n",
    "#A ~ b(N, Pa)，B ~ b(N, Pb)，其中Pa=0.1188，Pb=0.1205\n",
    "#根据中心极限定律，A和B可以近似为正态分布，那么，我们关注的随机变量X = (Pb–Pa)的分布也为正态分布\n",
    "#即   X ~ N (0, Pb(1-Pb)/N + Pa(1-Pa)/N)\n",
    "#根据标准分公式\n",
    "Pb = df_old_chang\n",
    "Pa = df_new_chang\n",
    "N = df_new['index'].count() \n",
    "Z = (Pb-Pa)/(np.sqrt((Pb*(1-Pb)+Pa*(1-Pa))/N))\n",
    "print(Z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9143969259585252"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查标准正太分布表格当Z = 1.368时候，用stats.norm.cdf函数求得置信区间\n",
    "df_zxd =stats.norm.cdf(Z)\n",
    "df_zxd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.08560307404147482"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求显著性P值\n",
    "P = 1-df_zxd\n",
    "P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P > 0.05,所以没有理由拒绝原假设，则原假设成立，所以新旧banner页面之间没有区别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#函数的封装\n",
    "def abtext(df,a): \n",
    "    #计算统计量\n",
    "    df_old = df[(df['landing_page']=='old_page')].reset_index()\n",
    "    df_new = df[(df['landing_page']=='new_page')].reset_index()\n",
    "    \n",
    "    df_new_sum =df_new['converted'].count() #计算新banner页总的点击量\n",
    "    df_new_suss = df_new.loc[:,'converted'].sum() #计算banner页转化的总数量\n",
    "    df_new_chang = df_new_suss/df_new_sum #计算新banner页面转化率\n",
    "    df_old_sum =df_old['converted'].count() #计算新banner页总的点击量\n",
    "    df_old_suss = df_old.loc[:,'converted'].sum() #计算banner页转化的总数量\n",
    "    df_old_chang = df_old_suss/df_old_sum #计算新banner页面转化率\n",
    "    \n",
    "     #计算Z的值\n",
    "    Pb = df_old_chang\n",
    "    Pa = df_new_chang\n",
    "    N = df_new['index'].count() \n",
    "    Z = (Pb-Pa)/(np.sqrt((Pb*(1-Pb)+Pa*(1-Pa))/N))\n",
    "    \n",
    "    #通过Z的值求出置信区间和概率P值\n",
    "    df_zxd =stats.norm.cdf(Z)\n",
    "    P = 1-df_zxd\n",
    "    \n",
    "    #比较P值和a值之间的关系如果P值大于a则原假设成立，如果P小于a则拒绝原假设\n",
    "    if P < a:\n",
    "        text='原假设不成立，两个页面之间有显著区别'\n",
    "    else :\n",
    "        text='原假设成立，两个页面之间没有显著区别'\n",
    "    \n",
    "    print(text,'\\n','统计量分别是%d和%d'%(df_new_sum,df_old_sum),'\\n','显著性P值为%f'%(P))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原假设成立，两个页面之间没有显著区别 \n",
      " 统计量分别是147239和147239 \n",
      " 显著性P值为0.085603\n"
     ]
    }
   ],
   "source": [
    "abtext(df,a=0.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
